﻿using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using FluentConfiguration = Scribble.Repositories.FluentConfiguration;

namespace Scribble.Tests.Scribble.Repositories
{
    [TestFixture]
    public abstract class RepositoryTestBase
    {
        protected ISession session;
        protected ITransaction transaction;

        private ISession GetTestSession()
        {
            var sessionFactory = Fluently.Configure()
                .Database(
                    MsSqlCeConfiguration.Standard
                        .ConnectionString("Data Source=Scribble/Repositories/RepositoryTestDatabase.sdf")
                )
                .Mappings(
                    m => m.AutoMappings.Add(AutoMap.AssemblyOf<Entity>(new FluentConfiguration()))
                )
                .ExposeConfiguration(c => new SchemaExport(c).Create(true, true))
                .BuildSessionFactory();

            return sessionFactory.OpenSession();
        }

        [SetUp]
        public void Setup()
        {
            session = GetTestSession();
            transaction = session.BeginTransaction();
            CreateDummyData();
            transaction.Commit();
        }

        [TearDown]
        public void Teardown()
        {
            transaction.Dispose();
            session.Dispose();
        }

        protected abstract void CreateDummyData();
    }
}